﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufItem.h"

/// \file GnufWorkspaceTabBar.h
/// \brief Workspace的C++接口层

namespace gnuf {

    class Toolbar;
    class WorkspaceTab;
    class QtWorkspaceTabBar;
    /// \brief Workspace 的顶部标签栏
    /// \see ItemWith QtWorkspaceTabBar
    class GNUF_BASE_EXPORT WorkspaceTabBar final
        : public ItemWith<QtWorkspaceTabBar>
    {
        CANNOT_COPY_OR_ASSIGN(WorkspaceTabBar);

    public:
        /// \brief Item类型的枚举值
        /// \see EItemType
        static const EItemType Type;

        /// \brief 构造函数
        /// \param pToolbar 标签栏所属的ToolBar
        /// \see Toolbar
        explicit WorkspaceTabBar(Toolbar *pToolbar);
        virtual ~WorkspaceTabBar();

        /// \brief WorkspaceTab 标签页在这个标签栏上的位置
        /// \param pWorkspaceTab 标签页的指针
        /// \return 返回索引值
        /// \see WorkspaceTab 
        int indexOf(const WorkspaceTab * pWorkspaceTab) const;

        /// \brief 设置当前标签页
        /// \param index 待设置标签页的索引值
        /// \param wantToDispatchEvent 是否想同时分发事件，默认为true
        void setCurrentIndex(const int index, const bool wantToDispatchEvent = true);

        /// \brief 返回当前标签页的索引值
        /// \return 当前标签页的索引值
        inline int currentIndex() const { return m_currentIndex; }

        /// \brief 返回当前的标签页
        /// \return 当前标签页的指针
        const WorkspaceTab * currentWorkspaceTab() const;
        WorkspaceTab * currentWorkspaceTab();

        /// \brief 返回指定位置的标签页
        /// \param index 指定的索引值
        /// \return 指定标签页的指针
        const WorkspaceTab * workspaceTabAt(const int index) const;
        WorkspaceTab * workspaceTabAt(const int index);
        
        /// \brief 返回标签页
        /// \param caption 标签页的名称Catpion。
        /// \return 标签页的指针。
        const WorkspaceTab * workspaceTabByCaption(const std::wstring & caption) const;
        WorkspaceTab *       workspaceTabByCaption(const std::wstring & caption);
        
        /// \brief 返回标签页
        /// \param workspaceKey 标签页的Key值。
        /// \return 标签页的指针。
        const WorkspaceTab * workspaceTabByKey(const std::wstring & workspaceKey) const;
        WorkspaceTab *       workspaceTabByKey(const std::wstring & workspaceKey);
        
        /// \brief 设置标签页是否可见。
        /// \param pTab 标签页的指针。
        /// \param visible 是否可见。
        bool setWorkspaceTabVisible(WorkspaceTab * pTab, const bool visible);

        /// \brief 添加标签页，标题和Key为系统自动生成
        WorkspaceTab * addWorkspaceTab();

        /// \brief 添加标签页
        /// \param caption 标签页的标题
        /// \param workspaceKey 标签页的唯一标识
        /// \param setAsCurrent 是否将添加的标签页设为当前标签页，默认为true
        /// \param immediatelyBuiltQt 是否立刻创建Qt控件，默认为true，
        /// - 通过代码添加标签页时，需要立刻创建Qt控件
        /// - 通过xml 文件配置添加标签页的时候，不需要立刻创建Qt控件
        /// \return 添加的标签页的指针
        /// \see WorkspaceTab
        WorkspaceTab* addWorkspaceTab(const std::wstring& caption, const std::wstring& workspaceKey,
            bool setAsCurrent = true, bool immediatelyBuildQt = true);

        /// \brief 删除指定位置的标签页
        /// \param index 指定需要删除的标签页的位置
        /// \return 返回当前的标签页
        /// \see WorkspaceTab
        WorkspaceTab* deleteWorkspaceTab(int index);

        /// \brief 删除指定key的标签页
        /// \param workspaceKey 需要删除的标签页的Key
        /// \return 返回当前的标签页
        /// \see WorkspaceTab
        WorkspaceTab* deleteWorkspaceTab(const std::wstring& workspaceKey);

        /// \brief 删除指定的标签页
        /// \param pTargetTab 需要删除的标签页
        /// \return 返回当前的标签页
        /// \see WorkspaceTab
        WorkspaceTab* deleteWorkspaceTab(WorkspaceTab* pTargetTab);

        /// \brief 返回最大WorkspaceTab数量
        /// \return 最大WorkspaceTab数量
        static int maxWorkspaceTabCount() ;

        /// \brief 设置最大WorkspaceTab数量，目前默认值是10个，产品根据需求调整, 最大支持50个
        /// \param maxCount 最大WorkspaceTab数量
        // TODO: 后续需要做自适应计算，根据主窗口宽度动态计算显示或者隐藏掉无法显示部分；
        static  void setMaxWorkspaceTabCount(const int maxCount);

        /// \brief 定义新类型Tabs，存放标签页的链表
        typedef std::list< gcmp::OwnerPtr<WorkspaceTab>> Tabs;

        /// \brief 定义新类型RawTabs，存放标签页的向量
        typedef std::vector< WorkspaceTab * > RawTabs;

        /// \brief 获取存放标签页的Vector
        /// \see RawTabs
        RawTabs rawTabs();

        /// \brief 获取标签页的链表
        /// \see Tabs
        inline const Tabs & tabs() const { return m_tabs; }

        const gcmp::OwnerPtr< Info > & showMoreInfo() const {
            return m_showMoreInfo;
        }

        virtual void transferContentsTo(Item * pTarget, 
            const Item::DecideTranferringContentMethod &, 
            const Item::PostContentTransferred &) override;
    protected:
        // Inherits from ItemWith...
        virtual void onDisconnectFromMyQtItem() override;

        virtual bool onAllowToTransferChild(
            const Item* pChild, const Item* pNewParent) const override;

    private:
        bool transferInAdvance(WorkspaceTab *, WorkspaceTabBar *, int );

        WorkspaceTabBar::Tabs m_tabs;
        gcmp::OwnerPtr< Info > m_showMoreInfo;
        int m_currentIndex;
        bool m_transferingChild;
    };

    class QtWorkspaceTab;

    /// \brief 顶部标题栏的标签页
    /// \see ItemWith QtWorkspaceTab
    class GNUF_BASE_EXPORT WorkspaceTab final : public ItemWith<QtWorkspaceTab>
    {
        CANNOT_COPY_OR_ASSIGN(WorkspaceTab)
    public:
        /// \brief Item类型的枚举值
        /// \see EItemType
        static const EItemType Type;

        /// \brief 构造函数
        /// \param pWorkspaceTabBar 标签页所在的父级标签栏的指针
        /// \see WorkspaceTabBar
        explicit WorkspaceTab(WorkspaceTabBar *pWorkspaceTabBar);
        virtual ~WorkspaceTab();

        /// \brief 返回标签页在标签栏上所处的位置
        int index() const;

        /// \brief 获取标签页的标题
        inline const std::wstring & caption() const { return m_caption; }

        /// \brief 设置标签页的标题
        /// \param caption 标题
        void setCaption(const std::wstring& caption);

        inline const std::wstring & workspaceKey() const { return m_workspaceKey; }
        /// \brief 设置标签页的Key
        /// \param workspaceKey Key值，宽字符类型
        void setWorkspaceKey(const std::wstring& workspaceKey) { m_workspaceKey = workspaceKey; }

    protected:
        // Inherits from ItemWith...
        virtual void onPostVisibilityChanged() override;
        virtual void onDisconnectFromMyQtItem() override;
        virtual void onPostAppearanceSettingChanged() override;
    private:
        std::wstring m_caption;
        std::wstring m_workspaceKey;
    };
}
